﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SC.eWedding.BusinessLogic.Views;
using SC.eWedding.BusinessLogic.Handlers;

public partial class Proveedor_Controles_ucCaracteristica : System.Web.UI.UserControl
{
    #region Propiedades Privadas

    private List<CaracteristicaView> Caracteristicas
    {
        get
        {
            if (Session["Caracteristicas"] == null)
                Session["Caracteristicas"] = new List<CaracteristicaView>();
            return (List<CaracteristicaView>)Session["Caracteristicas"];
        }
        set { Session["Caracteristicas"] = value; }
    }

    #endregion

    #region Eventos

    protected void lbAgregar_Click(object sender, EventArgs e)
    {
        if (txtNombre.Text != "" && !Caracteristicas.Exists(delegate(CaracteristicaView cv) { return cv.Nombre == txtNombre.Text; }))
        {
            CaracteristicaView caract = new CaracteristicaView();
            caract.Nombre = txtNombre.Text;
            caract.Detalle = txtDetalle.Text;
            caract.Id = -Caracteristicas.Count;
            Caracteristicas.Add(caract);
        }

        txtDetalle.Text = "";
        txtNombre.Text = "";

        CargarGrilla();
    }

    protected void gvCaracteristica_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gvCaracteristica.DataKeys[e.RowIndex].Values["Id"]);

        Caracteristicas.Remove(Caracteristicas.Find(delegate(CaracteristicaView c) { return c.Id == id; }));

        ResetearIdLista();
        CargarGrilla();
    }

    #endregion

    #region Metodos Privados

    private void ResetearIdLista()
    {
        int aux = 0;
        foreach (CaracteristicaView ca in Caracteristicas)
        {
            if (ca.Id <= 0)
            {
                ca.Id = aux;
                aux--;
            }
        }
    }

    #endregion

    #region Metodos Publicos

    public void CargarGrilla()
    {
        gvCaracteristica.DataSource = Caracteristicas;
        gvCaracteristica.DataBind();
    }

    #endregion
}
